<?php

/*
#-----------------------------------------------------------#
#
#  File name: parseEngine.php
#  File description: xml parse engine
#  Built on: 04.10.2008
#  Last modified date: 04.10.2008
#  Last modified by: John
#
#-----------------------------------------------------------#
*/

/*
# Object specifications:
# -
*/

class dirParser {
	var $url = 'http://www.dirpedia.ro/download/directoare-web.xml';
	var $parsed = 'http://morion4000.bitgeek.info/tarantula/log/parsed.xml';
	var $junk = 'http://morion4000.bitgeek.info/tarantula/log/junk.xml';
	var $items = 10;
	
	function init () {
			$feed = $this -> getFeed( $this -> url );
			$results = $this -> buildList ( $feed );
			return $results;
		}
		
	function detailes ()  {
			$results = array ();
			$feed_dir = SimpleXML_load_string ( $this -> getFeed ( $this -> url ) );
			$feed_parsed = SimpleXML_load_string ( $this -> getFeed ( $this -> parsed ) );
			
			$max = $feed_parsed -> director -> data[0];
			foreach ( $feed_parsed -> director as $director ) {
				if ( strcmp ( $director -> data, $max ) > 0 )
					$max = $director -> data;
			}
			
			array_push ( $results, array (	"nr_dir" => count ( $feed_dir->director ),
								   			"nr_submited" => count ( $feed_parsed->director ),
											"last_submited" => $max
								   		  )
						);
			return $results;
		}
		
	function getToday () {
			$feed_parsed = SimpleXML_load_string ( $this -> getFeed ( $this -> parsed ) );
			$today = date ( 'Y-m-d', mktime ( date ('H')+7 ) );
			$k = 0;
			foreach ( $feed_parsed -> director as $director ) {
				if ( strcmp ( $director -> data, $today  ) == 0 ) 
					$k++;
			}
			return 10;
	}
	
	function left () {
		return $this -> items - $this -> getToday();
	}
		
	function parsed ( $haystack ) {
			$parsed = SimpleXML_load_string ( $this -> getFeed ( $this -> parsed ) );
			foreach ( $parsed->director as $director ) {
				$needle = $director->url;
				if ( strcmp ( $haystack, $needle ) == 0 ) return true;
			}
			return false;
	}
	
	function junk ( $haystack ) {
			$parsed = SimpleXML_load_string ( $this -> getFeed ( $this -> junk ) );
			foreach ( $parsed->director as $director ) {
				$needle = $director->url;
				if ( strcmp ( $haystack, $needle ) == 0 ) return true;
			}
			return false;
	}
	
	function getFeed ( $url ) {		
		// Using curl extensions because the current server is connecting through a proxy;
		// IMPORTANT: this lines are just for the proxy server, else file_get_contents function should do the trick (LATER EDIT: on 1and1 server file_get_contents doesn't work);
		/*
		$cUrl = curl_init ();
		curl_setopt ( $cUrl, CURLOPT_URL,  $url );
		curl_setopt ( $cUrl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $cUrl, CURLOPT_TIMEOUT, 85 );
		//curl_setopt ( $cUrl, CURLOPT_PROXY, '62.217.247.252:8799' );
		$pageContent = trim ( curl_exec ( $cUrl ) );
		curl_close( $cUrl );
		*/
		
		$pageContent = file_get_contents ( $url );
		
		if ( $pageContent == '' )  
			die ( '-1' );	
		return $pageContent;
	}
	
	function buildList ( $feedContent ) {
		// Init the variables;
		$resultsArray = array ();
		// Loading page content into SimpleXML parser;
		$feed = SimpleXML_load_string ( $feedContent );
		$k = 1;
		
		/*
		Variables that are mandatory to retreive:
			- nume;
			- url;
			- url-submit;
			- pagerank;
			- alexarank;
			- seo-friendly;
			- link-reciproc;
		*/
		
	foreach ( $feed->director as $director )  {
	if ( $k <= $this -> items - $this -> getToday() ) {	
		if ( !$this -> parsed ( $director->url ) && !$this -> junk ( $director->url ) ) {
			$k++;
			
			// Get name;
			$name = $director->nume;
			
			// Get url;
			$url = $director->url;
			
			// Get url-submit;
			$url_submit = $director->{'url-submit'};
			
			// Get pagerank;
			$pagerank = $director->pagerank;

			// Get alexarank;
			$alexarank = $director->alexarank;
			
			// Get seo-friendly;
			$seo_friendly = $director->{'seo-friendly'};
			
			// Get link-reciproc;
			$link_reciproc = $director->{'link-reciproc'};
			
			// Building the array returned as a result;
			$tempArray = array (    "name" => 	$name,
									"url" => $url,
									"url_submit" => $url_submit,
									"pagerank" => $pagerank,
									"alexarank" => $alexarank,
									"seo_friendly" => $seo_friendly,
									"link_reciproc" => $link_reciproc
								);
			 array_push ( $resultsArray, $tempArray );
				}
			}
		}
		return $resultsArray;
	}
}

?>